package com.gxa.controller;

import com.gxa.common.R;
import com.gxa.service.CustomerService;
import com.gxa.service.GoodsService;
import com.gxa.service.SupplierService;
import com.gxa.service.ThreeService;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.sql.Timestamp;
import java.util.*;

/**
 * Created with IntelliJ IDEA.
 * User: 周林森
 * Date: 2021/12/02 15:25:41
 * Description: 基础资料
 * Version: V1.0
 */
@Controller
@Api(tags = "设置(基础资料)接口API")
@RequestMapping("/base_info")
public class BaseInfoController {

    @Autowired
    private CustomerService customerService;

    @Autowired
    private SupplierService supplierService;

    @Autowired
    private GoodsService goodsService;

    @Autowired
    private ThreeService threeService;

    /**
     * 客户管理
     */
    //1.添加新客户
    @RequestMapping(value = "/add_customer",method = RequestMethod.POST)
    @ApiOperation(value = "添加客户接口",notes = "添加客户",httpMethod = "POST",response = R.class)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "customer_Id",value = "客户编号",paramType = "query",dataType = "String",required = true),
            @ApiImplicitParam(name = "customer_Name",value = "客户名称",paramType = "query",dataType = "String",required = true),
            @ApiImplicitParam(name = "customer_Type_Id",value = "客户类别",paramType = "query",dataType = "String",required = true)
    })
    @ResponseBody
    public R addCustomer(@RequestBody Map map){
        System.out.println(map);
        Map map1 = (Map)map.get("newcustomer");
        System.out.println(map1);
        System.out.println("--------------------------");
        R r = customerService.addCustomer(map);
        return new R();
    }

    //2.查询客户
    @RequestMapping(value = "/query_customer_limit",method = RequestMethod.POST)
    @ApiOperation(value = "查询客户接口",notes = "查询客户",httpMethod = "POST",response = R.class)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page",value = "当前页码",paramType = "query",dataType = "String",required = false),
            @ApiImplicitParam(name = "limit",value = "当前条数",paramType = "query",dataType = "String",required = false),
            @ApiImplicitParam(name = "customer_Type_Id",value = "客户类别",paramType = "query",dataType = "String",required = false),
            @ApiImplicitParam(name = "input_value",value = "搜索输入框值",paramType = "query",dataType = "String",required = false)
    })
    @ResponseBody
    public R queryCustomer(@RequestBody Map map){
        R r = customerService.queryCustomer(map);
        return r;
    }

    //3.修改客户信息
    @RequestMapping(value = "/update_customer",method = RequestMethod.POST)
    @ApiOperation(value = "修改客户接口",notes = "修改客户",httpMethod = "POST",response = R.class)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "customer_Id",value = "客户编号",paramType = "query",dataType = "String",required = true),
            @ApiImplicitParam(name = "customer_Name",value = "客户名称",paramType = "query",dataType = "String",required = true),
            @ApiImplicitParam(name = "customer_Type_Id",value = "客户类别",paramType = "query",dataType = "String",required = true)
    })
    @ResponseBody
    public R updateCustomer(@RequestBody Map map){
        System.out.println(map);
        R result = customerService.updateCustomer(map);
        return result;
    }

    //4.多级、行级 删除客户
    @RequestMapping(value = "/del_customer",method = RequestMethod.POST)
    @ApiOperation(value = "删除客户接口",notes = "删除客户",httpMethod = "POST",response = R.class)
    @ResponseBody
    public R delCustomer(@RequestBody Map map){
        Object customer_id = map.get("customer_Id");
        R r = customerService.delCustomer(customer_id);
        return r;
    }

    //5.多级、行级 修改客户状态
    @RequestMapping(value = "/update_customers_status",method = RequestMethod.POST)
    @ApiOperation(value = "修改客户状态接口",notes = "修改客户",httpMethod = "POST",response = R.class)
//    @ApiImplicitParam(name = "customer_Id",value = "客户编号(数组)",paramType = "query",dataType = "String",required = true)
    @ResponseBody
    public R updateCustomerStatus(@RequestBody Map map){
        Object customer_id = map.get("customer_Id");
        Object status = map.get("status");
        R r = customerService.updateCustomerStatus(customer_id,status);
        return r;
    }


    /**
     * 供应商管理
     */
    //1.添加供应商
    @RequestMapping(value = "/add_supplier",method = RequestMethod.POST)
    @ApiOperation(value = "添加供应商接口",notes = "添加供应商",httpMethod = "POST",response = R.class)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "supplier_Id",value = "供应商编号",paramType = "query",dataType = "String",required = true),
            @ApiImplicitParam(name = "supplier_Name",value = "供应商名称",paramType = "query",dataType = "String",required = true),
            @ApiImplicitParam(name = "supplier_Type_Id",value = "供应商类别",paramType = "query",dataType = "String",required = true)
    })
    @ResponseBody
    public R addSupplier(@RequestBody Map map){
        System.out.println(map);
        R result = supplierService.addSupplier(map);
        return result;
    }

    //2.查询供应商
    @RequestMapping(value = "/query_supplier_limit",method = RequestMethod.POST)
    @ApiOperation(value = "查询供应商接口",notes = "查询供应商",httpMethod = "POST",response = R.class)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page",value = "当前页码",paramType = "query",dataType = "String",required = false),
            @ApiImplicitParam(name = "limit",value = "当前条数",paramType = "query",dataType = "String",required = false),
            @ApiImplicitParam(name = "supplier_Type_Id",value = "供应商类别",paramType = "query",dataType = "String",required = false),
            @ApiImplicitParam(name = "input_value",value = "搜索输入框值",paramType = "query",dataType = "String",required = false)
    })
    @ResponseBody
    public R querySupplier(@RequestBody Map map){
        R r = supplierService.querySupplier(map);
        return r;
    }

    //3.修改供应商信息
    @RequestMapping(value = "/update_supplier",method = RequestMethod.POST)
    @ApiOperation(value = "修改供应商接口",notes = "修改供应商",httpMethod = "POST",response = R.class)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "supplier_Id",value = "供应商编号",paramType = "query",dataType = "String",required = true),
            @ApiImplicitParam(name = "supplier_Name",value = "供应商名称",paramType = "query",dataType = "String",required = true),
            @ApiImplicitParam(name = "supplier_Type_Id",value = "供应商类别",paramType = "query",dataType = "String",required = true)
    })
    @ResponseBody
    public R updateSupplier(@RequestBody Map supplier){
        System.out.println(supplier);
        R result = supplierService.updateSupplier(supplier);
        return result;
    }

    //4.多级、行级 删除供应商
    @RequestMapping(value = "/del_supplier",method = RequestMethod.POST)
    @ApiOperation(value = "删除供应商接口",notes = "删除供应商",httpMethod = "POST",response = R.class)
    @ApiImplicitParam(name = "supplier_Id",value = "供应商编号(数组)",paramType = "query",dataType = "String",required = true)
    @ResponseBody
    public R delSupplier(@RequestBody Map map){
        Object supplier_id = map.get("supplier_Id");
        R r = supplierService.delSupplier(supplier_id);
        return r;
    }

    //5.多级、行级 修改供应商状态
    @RequestMapping(value = "/update_supplier_status",method = RequestMethod.POST)
    @ApiOperation(value = "修改供应商状态接口",notes = "修改供应商状态",httpMethod = "POST",response = R.class)
    @ApiImplicitParam(name = "supplier_Id",value = "供应商编号(数组)",paramType = "query",dataType = "String",required = true)
    @ResponseBody
    public R updateSupplierStatus(@RequestBody Map map){
        Object supplier_id = map.get("supplier_Id");
        Object status = map.get("status");
        R r = supplierService.updateSupplierStatus(supplier_id,status);
        return r;
    }


    /**
     * 商品管理
     */
    //1.添加商品
    @RequestMapping(value = "/add_goods",method = RequestMethod.POST)
    @ApiOperation(value = "添加商品接口",notes = "添加商品",httpMethod = "POST",response = R.class)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "goods_id",value = "商品编号",paramType = "query",dataType = "String",required = true),
            @ApiImplicitParam(name = "goods_name",value = "商品名称",paramType = "query",dataType = "String",required = true),
            @ApiImplicitParam(name = "goods_type_id",value = "商品类别",paramType = "query",dataType = "String",required = true),
            @ApiImplicitParam(name = "unit_id",value = "计量单位",paramType = "query",dataType = "String",required = true)
    })
    @ResponseBody
    public R addGoods(@RequestBody Map goods){
        System.out.println(goods);
        R r = goodsService.addGoods(goods);
        return r;
    }

    //商品图片上传
    @RequestMapping(value = "/saveUpload",method = RequestMethod.POST)
    public void saveUpload(MultipartFile img, HttpServletRequest request) {

        // 以当前日期创建一个文件夹，避免单个文件夹中文件过多
        Timestamp timestamp = new Timestamp(System.currentTimeMillis());
        // 截取年月日：2020-11-02
        String substring = timestamp.toString().substring(0, 10);
        // 设置文件上传存放的路径
        String uploadPath = request.getSession().getServletContext().getRealPath("/WEB-INF/upload/" + substring);

        System.out.println("获取到的图片上传地址为：" + uploadPath);
        // 获取上传文件名字
        String uploadName = img.getOriginalFilename();
        System.out.println("原始文件名：" + uploadName);
        // 利用UUID生成新的图片名字，避免原图片被覆盖
        String uuid = UUID.randomUUID().toString().replaceAll("-", "");

        // 截取上传文件的后缀
        String suffix = uploadName.substring(uploadName.lastIndexOf("."));
        // 拼接新的文件名字
        String newUploadName = uuid + suffix;
        System.out.println("新的文件名：" + newUploadName);

        File dir = new File(uploadPath, newUploadName);
        // exists() 测试此抽象路径名表示的文件或目录是否存在。
        if (!dir.exists()) {
            dir.mkdirs();
        }
        //MultipartFile自带的解析方法
        try {
            img.transferTo(dir);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    //2.查询商品
    @RequestMapping(value = "/query_goods_limit",method = RequestMethod.POST)
    @ApiOperation(value = "查询商品接口",notes = "查询商品",httpMethod = "POST",response = R.class)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page",value = "当前页码",paramType = "query",dataType = "String",required = false),
            @ApiImplicitParam(name = "limit",value = "当前条数",paramType = "query",dataType = "String",required = false),
            @ApiImplicitParam(name = "input_value",value = "搜索输入框值",paramType = "query",dataType = "String",required = false)
    })
    @ResponseBody
    public R queryGoods(@RequestBody Map map){
        R r = goodsService.queryGoods(map);
        return r;
    }

    //3.修改商品信息
    @RequestMapping(value = "/update_goods",method = RequestMethod.POST)
    @ApiOperation(value = "修改商品信息接口",notes = "修改商品信息",httpMethod = "POST",response = R.class)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "goods_id",value = "商品编号",paramType = "query",dataType = "String",required = true),
            @ApiImplicitParam(name = "goods_name",value = "商品名称",paramType = "query",dataType = "String",required = true),
            @ApiImplicitParam(name = "goods_type_id",value = "商品类别",paramType = "query",dataType = "String",required = true),
            @ApiImplicitParam(name = "unit_id",value = "计量单位",paramType = "query",dataType = "String",required = true)
    })
    @ResponseBody
    public R updateGoods(@RequestBody Map map){
        System.out.println(map);
        R r = goodsService.updateGoodsInfo(map);
        return r;
    }

    //4.多级、行级 删除商品
    @RequestMapping(value = "/del_goods",method = RequestMethod.POST)
    @ApiOperation(value = "删除商品接口",notes = "删除商品",httpMethod = "POST",response = R.class)
    @ApiImplicitParam(name = "goods_Id",value = "商品编号(数组)",paramType = "query",dataType = "String",required = true)
    @ResponseBody
    public R delGoods(@RequestBody Map map){
        Object goods_id = map.get("goods_Id");
        R r = goodsService.delGoods(goods_id);
        return r;
    }

    //5.多级、行级 修改商品状态
    @RequestMapping(value = "/update_goods_status",method = RequestMethod.POST)
    @ApiOperation(value = "修改商品状态接口",notes = "修改商品状态",httpMethod = "POST",response = R.class)
    @ApiImplicitParam(name = "goods_Id",value = "商品编号(数组)",paramType = "query",dataType = "String",required = true)
    @ResponseBody
    public R updateGoodsStatus(@RequestBody Map map){
        Object goods_id = map.get("goods_Id");
        Object status = map.get("status");
        R r = goodsService.updateGoodsStatus(goods_id,status);
        return r;
    }

    //首页商品库存预警查询
    @RequestMapping(value = "/GoodsWarning",method = RequestMethod.POST)
    @ApiOperation(value = "首页商品库存预警查询",notes = "",httpMethod = "POST",response = R.class)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page",value = "当前页码",paramType = "query",dataType = "String",required = false),
            @ApiImplicitParam(name = "limit",value = "页条数",paramType = "query",dataType = "String",required = false),
            @ApiImplicitParam(name = "warehouse",value = "仓库名",paramType = "query",dataType = "String",required = false),
            @ApiImplicitParam(name = "upLow",value = "低、高",paramType = "query",dataType = "String",required = false),
            @ApiImplicitParam(name = "input",value = "输入框",paramType = "query",dataType = "String",required = false),
    })
    @ResponseBody
    public R queryGoodsWarning(@RequestBody Map map){
        R r = goodsService.queryWarning(map);
        return r;
    }




    /**
     * 仓库管理
     */
    //1.查询仓库信息显示
//    @RequestMapping(value = "/query_warehouse_limit",method = RequestMethod.GET)
//    @ApiOperation(value = "查询仓库接口",notes = "查询仓库",httpMethod = "GET",response = R.class)
//    @ResponseBody
//    public R queryWarehouse(@RequestParam(defaultValue = "1") Integer page,@RequestParam(defaultValue = "10") Integer limit){
//        R r = new R();
//        r.setMsg("分页查询成功");
//        r.setCode(200);
//        List<Warehouse> warehouseList = new ArrayList<>();
//        Warehouse warehouse = new Warehouse();
//        warehouse.setWarehouse_Name("上海小周仓库");
//        warehouse.setWarehouse_Id(565);
//        warehouse.setStatus("启用");
//        Warehouse warehouse1 = new Warehouse();
//        warehouse.setWarehouse_Name("重庆小周仓库");
//        warehouse1.setWarehouse_Id(468);
//
//        warehouseList.add(warehouse);
//        warehouseList.add(warehouse1);
//        r.setData(warehouseList);
//        return r;
//    }

    //2.添加仓库
    @RequestMapping(value = "/add_warehouse",method = RequestMethod.POST)
    @ApiOperation(value = "添加仓库接口",notes = "添加仓库",httpMethod = "POST",response = R.class)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "warehouse_Name",value = "仓库名称",paramType = "query",dataType = "String",required = true),
    })
    @ResponseBody
    public R addWarehouse(@RequestBody Map map){
        Object warehouse_name = map.get("warehouse_Name");
        R r = threeService.addWarehouse(warehouse_name);
        return r;
    }

    //3.修改仓库信息
    @RequestMapping(value = "/update_warehouse",method = RequestMethod.POST)
    @ApiOperation(value = "修改仓库信息接口",notes = "修改仓库信息",httpMethod = "POST",response = R.class)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "warehouse_Id",value = "仓库编号",paramType = "query",dataType = "String",required = true),
            @ApiImplicitParam(name = "warehouse_Name",value = "仓库名称",paramType = "query",dataType = "String",required = true),
    })
    @ResponseBody
    public R updateWarehouse(@RequestBody Map map){
        Object warehouse_id = map.get("warehouse_Id");
        Object warehouse_name = map.get("warehouse_Name");
        R r = threeService.updateWarehouse(warehouse_id,warehouse_name);
        return r;
    }

    //4.行级 删除仓库
    @RequestMapping(value = "/del_warehouse",method = RequestMethod.POST)
    @ApiOperation(value = "删除仓库接口",notes = "删除仓库",httpMethod = "POST",response = R.class)
    @ApiImplicitParam(name = "warehouse_Id",value = "仓库编号",paramType = "query",dataType = "String",required = true)
    @ResponseBody
    public R delWarehouse(@RequestBody Map map){
        Object warehouse_id = map.get("warehouse_Id");
        R r = threeService.delWarehouse(warehouse_id);
        return r;
    }


    /**
     * 职员管理
     */
    //1.查询职员信息显示
//    @RequestMapping(value = "/query_employee_limit",method = RequestMethod.GET)
//    @ApiOperation(value = "查询职员接口",notes = "查询职员",httpMethod = "GET",response = R.class)
//    @ResponseBody
//    public R queryEmployee(@RequestParam(defaultValue = "1") Integer page,@RequestParam(defaultValue = "10") Integer limit){
//        R r = new R();
//        r.setMsg("分页查询成功");
//        r.setCode(200);
//        List<Staff_Member_Manage> list = new ArrayList<>();
//        Staff_Member_Manage warehouse = new Staff_Member_Manage();
//        warehouse.setStaff_Member_Id(520);
//        warehouse.setStaff_Member_Name("小周");
//        warehouse.setStatus("启用");
//        Staff_Member_Manage warehouse1 = new Staff_Member_Manage();
//        warehouse.setStaff_Member_Name("重庆小周");
//        warehouse1.setStaff_Member_Id(468);
//        warehouse1.setStatus("禁用");
//        list.add(warehouse);
//        list.add(warehouse1);
//        r.setData(list);
//        return r;
//    }

    //2.添加职员
    @RequestMapping(value = "/add_employee",method = RequestMethod.POST)
    @ApiOperation(value = "添加职员接口",notes = "添加职员",httpMethod = "POST",response = R.class)
    @ApiImplicitParams({
//            @ApiImplicitParam(name = "staff_Member_Id",value = "职员编号",paramType = "query",dataType = "String",required = true),
            @ApiImplicitParam(name = "staff_Member_Name",value = "职员名称",paramType = "query",dataType = "String",required = true),
    })
    @ResponseBody
    public R addEmployee(@RequestBody Map map){
        Object staff_member_name = map.get("staff_Member_Name");
        R r = threeService.addStaff(staff_member_name);
        return r;
    }

    //3.修改职员信息
    @RequestMapping(value = "/update_employee",method = RequestMethod.POST)
    @ApiOperation(value = "修改职员信息接口",notes = "修改职员信息",httpMethod = "POST",response = R.class)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "staff_Member_Id",value = "职员编号",paramType = "query",dataType = "String",required = true),
            @ApiImplicitParam(name = "staff_Member_Name",value = "职员名称",paramType = "query",dataType = "String",required = true),
    })
    @ResponseBody
    public R updateEmployee(@RequestBody Map map){
        Object staff_member_id = map.get("staff_Member_Id");
        Object staff_member_name = map.get("staff_Member_Name");
        R r = threeService.updateStaff(staff_member_id,staff_member_name);
        return r;
    }

    //4.行级 删除职员
    @RequestMapping(value = "/del_employee",method = RequestMethod.POST)
    @ApiOperation(value = "删除职员接口",notes = "删除职员",httpMethod = "POST",response = R.class)
    @ApiImplicitParam(name = "staff_Member_Id",value = "职员编号",paramType = "query",dataType = "String",required = true)
    @ResponseBody
    public R delEmployee(@RequestBody Map map){
        Object staff_member_id = map.get("staff_Member_Id");
        R r = threeService.delStaff(staff_member_id);
        return r;
    }


    /**
     * 账户管理
     */
    //1.查询账户信息显示
//    @RequestMapping(value = "/query_account_manage_limit",method = RequestMethod.GET)
//    @ApiOperation(value = "查询账户接口",notes = "查询账户",httpMethod = "GET",response = R.class)
//    @ResponseBody
//    public R queryAccount_Manage(@RequestParam(defaultValue = "1") Integer page,@RequestParam(defaultValue = "10") Integer limit){
//        R r = new R();
//        r.setMsg("分页查询成功");
//        r.setCode(200);
//        return r;
//    }

    //2.添加账户
    @RequestMapping(value = "/add_account_manage",method = RequestMethod.POST)
    @ApiOperation(value = "添加账户接口",notes = "添加账户",httpMethod = "POST",response = R.class)
    @ApiImplicitParams({
//            @ApiImplicitParam(name = "account_Id",value = "账户编号",paramType = "query",dataType = "String",required = true),
            @ApiImplicitParam(name = "account_Name",value = "账户名称",paramType = "query",dataType = "String",required = true),
            @ApiImplicitParam(name = "creat_Date",value = "账户日期",paramType = "query",dataType = "String",required = true),
    })
    @ResponseBody
    public R addAccount_Manage(@RequestBody Map map){
        R r = threeService.addAccount(map);
        return r;
    }

    //3.修改账户信息
    @RequestMapping(value = "/update_account_manage",method = RequestMethod.POST)
    @ApiOperation(value = "修改账户信息接口",notes = "修改账户信息",httpMethod = "POST",response = R.class)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "account_Id",value = "账户编号",paramType = "query",dataType = "String",required = true),
            @ApiImplicitParam(name = "account_Name",value = "账户名称",paramType = "query",dataType = "String",required = true),
            @ApiImplicitParam(name = "creat_Date",value = "账户名称",paramType = "query",dataType = "String",required = true),
    })
    @ResponseBody
    public R updateAccount_Manage(@RequestBody Map map){
        R r = threeService.updateAccount(map);
        return r;
    }

    //4.行级 删除账户
    @RequestMapping(value = "/del_account_manage",method = RequestMethod.POST)
    @ApiOperation(value = "删除账户接口",notes = "删除账户",httpMethod = "POST",response = R.class)
    @ApiImplicitParam(name = "account_Id",value = "账户编号",paramType = "query",dataType = "String",required = true)
    @ResponseBody
    public R delAccount_Manage(@RequestBody Map map){
        Object account_id = map.get("account_Id");
        R r = threeService.delAccount(account_id);
        return r;
    }






}
